package com.yonyou.pmclouds.plan.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.entity.ProjectPlanVO;
import com.yonyou.pmclouds.plan.mapper.ProjectPlanMapper;

public class PlanInsertPermCheck implements SingleProcessor<ProjectPlanVO> {
    private ProjectPlanMapper mapper;

    public PlanInsertPermCheck(ProjectPlanMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void process(ProjectPlanVO vo, ProjectPlanVO orig) throws BusinessException {
        if (vo.getPkParent() == null) {
            // 根节点不需要校验
            return;
        }
        String rootId = vo.getPkRoot();
        ProjectPlanVO root = mapper.getById(rootId);
        String creator = root.getCreator();
        if (vo.getCreator() == null || !vo.getCreator().equals(creator)) {
            throw new BusinessException("当前用户不是该新建计划的根计划的创建人，不可新建！");
        }
    }
}
